package com.auto.survey.activity.monitoring.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.auto.survey.R
import com.auto.survey.activity.monitoring.entity.relations.PointWithCheckedPoint
import com.auto.survey.base.CustomApplication

class AutoCheckAdapter : Adapter<AutoCheckAdapter.AutoCheckViewHodler>() {

    private var mItems :List<PointWithCheckedPoint> = mutableListOf()
    private var listener:AutoCheckAdapterClickListener? = null

    fun setData(items:List<PointWithCheckedPoint>) {
        mItems = items
        notifyDataSetChanged()
    }

    fun setOnAdapterClick(listen:AutoCheckAdapterClickListener) {
        listener = listen
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AutoCheckViewHodler {

        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_auto_checked, parent, false)
        return  AutoCheckViewHodler(view)
    }

    override fun getItemCount(): Int {
        if (mItems.isNotEmpty()) {
            return mItems.size
        }
        return 0
    }

    override fun onBindViewHolder(holder: AutoCheckViewHodler, position: Int) {
        holder.pointname.text = mItems[position].point.pointName

        if (mItems[position].latestCheck.firstOrNull()?.isChecked == true) {
            holder.state.setTextColor(ContextCompat.getColor(CustomApplication.getContext(), android.R.color.holo_green_dark))
            holder.state.text = "已核验"
        } else {
            holder.state.setTextColor(ContextCompat.getColor(CustomApplication.getContext(), android.R.color.holo_red_dark))
            holder.state.text = "未核验"
        }


        holder.dorestchecked.setOnClickListener { listener?.onResetCheckStauts(position, mItems[position]) }
        holder.dobeginchecked.setOnClickListener { listener?.onStartCheck(position, mItems[position])  }
        holder.doNoAutochecked.setOnClickListener { listener?.commonCheck(position, mItems[position]) }
    }

    inner class AutoCheckViewHodler(itemview:View) : ViewHolder(itemview) {
        val pointname = itemview.findViewById<TextView>(R.id.tv_autocheck_item_pointname)
        val state = itemview.findViewById<TextView>(R.id.tv_autocheck_item_state)

        val dorestchecked = itemview.findViewById<TextView>(R.id.tv_reset_item)
        val dobeginchecked = itemview.findViewById<TextView>(R.id.tv_startcheck_item)
        val doNoAutochecked = itemview.findViewById<TextView>(R.id.tv_noautocheck_item)
    }

    interface AutoCheckAdapterClickListener{
        fun onResetCheckStauts(position: Int, item:PointWithCheckedPoint)

        fun onStartCheck(position: Int, item:PointWithCheckedPoint)

        fun commonCheck(position: Int, item:PointWithCheckedPoint)
    }
}